package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ViewUser
 */
@WebServlet("/ViewUser")
public class ViewUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String vp = request.getParameter("p");
		if(vp.equals("1")){
			request.setAttribute("output", "<br>You selected list all.");
			String all = listAllRecords();
			request.setAttribute("output",all);
		}
		request.getRequestDispatcher("viewuser.jsp").
		forward(request,response);
	}
		String listAllRecords(){
			
				String output ="";
				try {
					Class.forName("org.sqlite.JDBC");
					String url = "jdbc:sqlite:c:/Users/JayeM/workspace/MyWebProject/MyWebProject/WebContent/database/database.db";
					Connection con = DriverManager.getConnection(url);
					
					String sql = "select * from tbluserinfo where usertype='regular';";
					PreparedStatement pstmt = con.prepareStatement(sql);
					ResultSet rlt = pstmt.executeQuery();
					
					while(rlt.next()){
						output+="<br>ID: "+rlt.getInt(1);
						output+="<br>Username: "+rlt.getString(2);
						output+="<br>Password: "+rlt.getString(3);
						output+="<br>Usertype: "+rlt.getString(4);
						output+="<br>Firstname: "+rlt.getString(5);
						output+="<br>Lastname: "+rlt.getString(6);
						output+="<br>Email Address: "+rlt.getString(7);
						
					}
					
					pstmt.close();
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
		return output;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
